﻿/*删除*/
function member_del(data) {
    if (data.Count>0) {
        layer.alert("存在用户,不能删除");
        return false;
    } else {
        $.ajax({
            url: "/Role/DelRole?RoleId=" + data.Id,
            type: "post",
            success: function (data) {
                if (data.Success) {
                    layer.alert('删除成功！', {
                        title: '提示框',
                        icon: 6,
                    });
                    layui.table.reload('sample-table-1', {//就会读取后台数据,重新加载；
                        where: {                            
                        }
                    });
                } else {
                    layer.alert('删除失败！', {
                        title: '提示框',
                        icon: 5,
                    });
                }
            }
        });
    }
}


/*字数限制*/
function checkLength(which) {
    var maxChars = 200; //
    if (which.value.length > maxChars) {
        layer.open({
            icon: 2,
            title: '提示框',
            content: '您出入的字数超多限制!',
        });
        // 超过限制的字数了就将 文本框中的内容按规定的字数 截取
        which.value = which.value.substring(0, maxChars);
        return false;
    } else {
        var curr = maxChars - which.value.length; //250 减去 当前输入的
        document.getElementById("sy").innerHTML = curr.toString();
        return true;
    }
};
//面包屑返回值
var index = parent.layer.getFrameIndex(window.name);
parent.layer.iframeAuto(index);
$('.Order_form ,#Competence_add').on('click', function () {
    var cname = $(this).attr("title");
    var cnames = parent.$('.Current_page').html();
    var herf = parent.$("#iframe").attr("src");
    parent.$('#parentIframe span').html(cname);
    parent.$('#parentIframe').css("display", "inline-block");
    parent.$('.Current_page').attr("name", herf).css({ "color": "#4c8fbd", "cursor": "pointer" });
    //parent.$('.Current_page').html("<a href='javascript:void(0)' name="+herf+">" + cnames + "</a>");
    parent.layer.close(index);

});

layui.use('table', function () {
    var table = layui.table,
        form = layui.form;

    //第一个实例
    table.render({
        elem: '#sample-table-1'
        , height: 480
        , url: '/Role/GetRoleUser' //数据接口                                    
        , page: true //开启分页
        , cols: [[ //表头
            
             { field: 'Name', width: 150, title: '权限名称', sort: true, fixed: 'left', align: 'center' }
            , { field: 'Count', title: '人数', width: 80, align: 'center' }
            , { field: 'UserName', title: '用户名称', sort: true, align: 'center' }
            , { field: 'Remark', title: '描述', width: 150, align: 'center' }
            , { field: 'CreateTime', title: '创建时间', sort: true, align: 'center', templet: "<div>{{Format(d.CreateTime, 'yyyy年MM月dd日 hh:mm:ss')}}</div>" }
            , { field: 'IsEnable', title: '状态', width: 100, templet: '#switchTpl', unresize: true, align: 'center' }
            , { title: '操作', toolbar: '#barDemo', width: 150, align: 'center' }
        ]]
    });


    //监听工具条
    table.on('tool(test)', function (obj) {
        var data = obj.data;
        if (obj.event === 'del') {
            layer.confirm('真的删除么', function (index) {                
                member_del(data);                
            });

        } else if (obj.event === 'edit') {            
            var index = layer.open({
                type: 2,
                maxmin: true,
                content: ['/Role/UpdateCompetence?Id=' + data.Id, "no"],
            });      
            layer.full(index);
            //member_edit(data, index);
            //layer.alert('编辑行：<br>' + JSON.stringify(data))    
        }
    });

    //修改状态
    form.on('switch(statusDemo)', function (obj) {
        $.ajax({
            url: "/Role/UpdateIsEnableById",
            data: { RoleId: this.value, IsEnable: obj.elem.checked },
            type: "post",
            success: function (data) {
                if (data.Success) {
                    layer.alert('修改成功！', {
                        title: '提示框',
                        icon: 6,
                    });
                } else {
                    layer.alert('修改失败！', {
                        title: '提示框',
                        icon: 5,
                    });
                }
            }
        });
    });

    //复选框删除
    $("#btn-danger").click(function () {
        var checkStatus = table.checkStatus('sample-table-1');
        data = checkStatus.data;
        if (data.length < 0) {
            layer.alert("请先选择数据");
        } else {
            data.forEach(function (item) {
                member_del(item);
            })
            location.reload();
        }
    })
});